<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="mycrmprkaushik.Customer" %>
<%@ page import="mycrmprkaushik.PMF" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="java.util.List" %>


<html>
<body>

			<%
						
			String keystring = request.getParameter("key");
			PersistenceManager editpm = PMF.get().getPersistenceManager();
			
			Customer editcust = editpm.getObjectById(Customer.class, Long.parseLong(keystring));
		
			%>
						
			<form action="/addcust" method="post">
	   		<table border="0">
			<tr><td><align="right"><div class="formtext">Name:</div></td>
			<td><input type =text name="name" size=25 value="<%=editcust.getName()%>"></td>			
			<td><align="right">Company:</td>
			<td><input type =text name="company" size=25 value="<%=editcust.getCompany()%>"></td></tr>
			<tr><td><align="right">Phone:</td><td><input type =text name="phone" size=25 value="<%=editcust.getPhone()%>"></td>		
			<td><align="right">Address:</td><td><input type =text name="address" size=25 value ="<%=editcust.getAddress()%>">
			<input type="submit" value="Create Customer" /></td>		
			<input type="hidden" name="key" value="<%=editcust.getKey()%>" /></td>		
			</tr>		   
     		</table>
    		</form>

				My Customers 
				<%
				PersistenceManager pm = PMF.get().getPersistenceManager();
				String query = "select from " + Customer.class.getName();
				List<Customer> customers = (List<Customer>) pm.newQuery(query).execute();
				%>				
				<%
				if (customers.isEmpty()) {
				%>
					<p>No Customers</p>
				<%
					} else {
				%>
					<p></p>
				<%
					for (Customer cust : customers) {
						Long custkey = cust.getKey();
						String urldelete = "/deletecust.jsp?key=" + custkey.toString();
						String urledit = "/editcust.jsp?key=" + custkey.toString();
				%>
					<td><%=cust.getName()%> <%=cust.getAddress()%></td>
					<td><%=cust.getCompany()%> <%=cust.getPhone()%></td>
					<a href='<%=urldelete%>'>Delete</a>&nbsp;
				    <a href='<%=urledit%>'>Edit</a>
					<p>	
 
 				<%
									}
					}
				%>
</body>
</html>